<template>
  <div class="add-form">
    <el-dialog :title="text" :visible.sync="dialogFormVisible" :before-close="dialogFormH">
      <el-form
        :rules="ruleInline"
        ref="dataForm"
        :model="formBase"
        label-position="left"
        label-width="120px"
        style="width: 400px; margin-left:120px;"
      >

        <el-form-item label="用户名" prop="username">
          <el-input v-model="formBase.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formBase.email" ></el-input>
        </el-form-item>
        <el-form-item
        label="密码"
        prop="password"
        v-if="!formBase.id"
        >
          <el-input v-model="formBase.password"></el-input>
        </el-form-item>

        <!-- 角色 -->
        <el-form-item label="角色" >
          <el-input v-model="formBase.role"></el-input>
        </el-form-item>
        <!-- 权限组 -->
        <el-form-item label="权限组名称">
          <el-select v-model.number="formBase.permission_group_id" placeholder="请选择">
            <el-option :value="item.title" v-for="item in simpl" :key="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="联系电话" >
          <el-input v-model="formBase.phone" ></el-input>
        </el-form-item>

        <!-- 头像上传下一个版本再做 -->
        <!-- <el-form-item :label="$t('table.avatar')" prop="avatar">
            <el-upload
              class="upload-demo"
              :action="importFileUrl"
              :on-change="handleChange"
              :file-list="fileList" accept="image/jpeg,image/gif,image/png,image/bmp"
              :before-upload="beforeAvatarUpload">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
        </el-form-item>-->
        <el-form-item label="介绍">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="Please input"
            v-model="formBase.introduction"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormH">{{$t('table.cancel')}}</el-button>
        <el-button type="primary" @click="createData">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { detail, update, add } from '@/api/base/users'
export default {
  name: 'usersAdd',
  props: {
    text: {
      type: String,
      required: true
    },
    simpl: {
      type: Array,
      required: true
    },
    details: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      dialogFormVisible: false,
      formBase: {
        avatar: '',
        role: '',
        phone: '',
        permission_group_id: '',
        email: '',
        password: '',
        username: '',
        introduction: ''
      },
      ruleInline: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  methods: {
    // 弹层显示
    dialogFormV () {
      this.dialogFormVisible = true
    },
    // 弹层隐藏
    dialogFormH () {
      this.formBase.permission_group_id = ''
      this.formBase.role = ''
      this.formBase.phone = ''
      this.formBase.introduction = ''
      this.$refs.dataForm.resetFields()
      this.dialogFormVisible = false
    },

    // 表单提交
    async createData () {
      await this.$refs.dataForm.validate()
      if (this.text === '创建用户') {
        await add(this.formBase)
        this.dialogFormH()
        this.$parent.userLis()
      } else {
        update(this.formBase).then(() => {
          this.dialogFormH()
          this.$parent.userLis()
        })
      }
      // this.dialogFormH()
      // this.$parent.userLis()
    },
    // 发请求获取详情
    async updata () {
      const { data } = await detail(this.details)
      // console.log(data)
      this.formBase = {
        avatar: data.avatar,
        role: data.role,
        phone: data.phone,
        permission_group_id: data.permission_group_id,
        email: data.email,
        username: data.username,
        introduction: data.introduction,
        id: data.id
      }
    }
  },
  // 挂载结束
  mounted: function () {},
  // 创建完毕状态
  async created () {
  },
  // 组件更新
  updated: function () {}
}
</script>
<style>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center
}
</style>
